# This coding file calculates descriptive
# statistics for Appendix Tables A.1-A.5
# File shouldbe run sequentially after coding files 1-4

############################################################################
# Table A.1: 2011 Covariate Means on Matched Variables in Final 20 subset
############################################################################

#load village-level descriptive data from 2011 for village assigned to a matched pair
#Insert correct file path
data20 <- read.csv("Data.VillageMatching2011.csv")

#Calculate mean of each variable in the list for conflict and non-conflict villages
list.v <- c("genderatt11.mean", "mistreat11.mean", "violence11.mean", "domviol.mean", "sexviol.mean", "earnincome.mean", "cotrust.mean", "trustcoethmore.mean", "stolenbike.mean", "pca1.mean", "pca2.mean", "pca3.mean", "ef1", "forest", "mountains", "mine", "T1")

MatchedMeans0 <- apply(data20[data20$warsince2011==0,c(list.v)], 2, mean)
MatchedMeans1 <- apply(data20[data20$warsince2011==1,c(list.v)], 2, mean)
MatchedMeansdiff <- MatchedMeans1-MatchedMeans0
MatchedMin <- apply(data20[,c(list.v)], 2, min)
MatchedMax <- apply(data20[,c(list.v)], 2, max)

# Statistical test of difference between conflict and non-conflict villages
MatchedP <- matrix(NA, nrow=length(list.v), ncol=1)
for (i in 1:length(list.v)){
  test1<- t.test(data20[data20$warsince2011==0, list.v[i]],data20[data20$warsince2011==1, list.v[i]], conf.level = 0.95)
  MatchedP[i,1] <- round(as.numeric(test1$p.value),3)
} 

# Generate Table Descriptives
nam <- c("Gender Attitudes", "Mistreat Women", "Tolerate VAW", "Domestic Violence", "Sexual Violence", "Earn Income",
         "Coethnic trust", "Trust Coethnics More", "Stolen Bike (security)", "Wealth PCA1", "Wealth PCA2", "Wealth PCA3",
         "Ethnic Fractionalization", "Forest", "Mountains", "Mine", "Tuungane Intervention")

balance20 <- data.frame(nam,(round(cbind(MatchedMin, MatchedMax, MatchedMeans0, MatchedMeans1, MatchedMeansdiff, MatchedP),3)))

# Output Table

kable(balance20, row.names = F,"rst", booktabs = T, col.names=c("", "Min", "Max", "Mean (NC)", "Mean (C)", "Diff", "P(Diff)"), caption ="2011 Covariate Means on Matched Variables in Final 20 subset")%>% 
  kable_styling(latex_options = c("scale_down", "hold_position"))

############################################################################
# Tables A.2-A.5: Calculations
############################################################################

# summary statistics for individuals in focus group

# list of outcome variables
alllist <- c("PRIV1.RC2.VIOL", "PUB1.RC2.VIOL","GCHOIX.RC2.VIOL", "PRIV2.RC2.VIOL",
             "PRIV1.RC2.DV", "PUB1.RC2.DV","GCHOIX.RC2.DV", "PRIV2.RC2.DV",
             "PRIV1.RC2.VOL", "PUB1.RC2.VOL","GCHOIX.RC2.VOL", "PRIV2.RC2.VOL",
             "FGMEAN.PRIV1.VIOL", "FGMEAN.PRIV2.VIOL",
             "FGMEAN.PRIV1.DV" , "FGMEAN.PRIV2.DV",
             "FGMEAN.PRIV1.VOL", "FGMEAN.PRIV2.VOL"
)


# list of covariates
covlist <- c("AGE","EDU", "VIVEVILLN", "RENCONAN", "HOMOGENE", "GUERREAPRES2011", "LEVIOL.FG","LAVIOLENCEDOMESTIQUE.FG", "LEVOL.FG")

# function summarizing each variable
statfct <- function(c, l){
  
  mat <- matrix(NA, nrow=length(l), ncol=c)
  
  for (i in 1:length(l)){
    mat[i,1] <- round(min(D3[,l[i]], na.rm = TRUE), 2)
    mat[i,2] <- round(max(D3[,l[i]], na.rm = TRUE), 2)
    
    mat[i,3] <- length(na.omit(D3[,l[i]]))
    mat[i,4] <- round(mean(D3[,l[i]], na.rm = TRUE), 2)
    mat[i,5] <- round(sd(D3[,l[i]], na.rm = TRUE), 2)
    
    mat[i,6] <- length(na.omit(D3[D3$FEMALE==1,l[i]]))
    mat[i,7] <- round(mean(D3[D3$FEMALE==1,l[i]], na.rm = TRUE), 2)
    mat[i,8] <- round(sd(D3[D3$FEMALE==1,l[i]], na.rm = TRUE), 2)
    
    mat[i,9] <- length(na.omit(D3[D3$FEMALE==0,l[i]]))
    mat[i,10] <- round(mean(D3[D3$FEMALE==0,l[i]], na.rm = TRUE), 2)
    mat[i,11] <- round(sd(D3[D3$FEMALE==0,l[i]], na.rm = TRUE), 2)
    test1<- t.test(D3[D3$FEMALE==1, l[i]], D3[D3$FEMALE==0, l[i]], conf.level = 0.95)
    mat[i,12] <- round(as.numeric(test1$p.value),3)
  } 
  row.names(mat) <- as.character(l)
  colnames(mat) <- c("MIN", "MAX", "N","MEAN", "SD", "N(F)", "MEAN(F)", "SD(F)","N(M)","MEAN(M)", "SD(M)","P(DIFF)")
  
  return(mat)
}

## apply and name data frame rows for outcome variables and covariates
outstats.mf <- statfct(c=12, l=alllist)
row.names(outstats.mf) <- alllist

covstats.mf <- statfct(c=12, l=covlist)
row.names(covstats.mf) <- covlist

############################################################################
# Table A.2: Summary Characteristics of All Participants
############################################################################

kable(covstats.mf[,c("MIN", "MAX", "N", "MEAN", "SD")], "rst", digits=c(0,0,0,2,2), booktabs = T, caption="Summary Characteristics of All Participants", row.names=TRUE)

############################################################################
# Table A.3: Summary Characteristics of Female and Male Participants
############################################################################
kable(covstats.mf[,c("N(F)", "MEAN(F)", "SD(F)", "N(M)", "MEAN(M)", "SD(M)")], "rst", digits=c(0,2,2,0,2,2), booktabs = T, caption="Summary Characteristics of Female and Male Participants", row.names=TRUE)


############################################################################
# Table A.4: Preference Variables for All Participants
############################################################################
kable(outstats.mf[,c("MIN", "MAX", "N", "MEAN", "SD")], "rst", digits=c(2,2,0,2,2), booktabs = T, caption="Preference Variables for All Participants", row.names = TRUE)

############################################################################
# Table A.5: Preference Variables for Female and Male Participants
############################################################################
kable(outstats.mf[,c("N(F)", "MEAN(F)", "SD(F)", "N(M)", "MEAN(M)", "SD(M)")], "rst", digits=c(0,2,2,0,2,2), booktabs = T, caption="Preference Variables for Female and Male Participants", row.names = TRUE)









